import React, { Component } from 'react';
import { track, Chart, Axis, Tooltip, Geom, Legend } from '../../../assets/lib/BizCharts.min';
// import {track} from 'bizcharts/lib/index';
// import Chart from 'bizcharts/lib/components/Chart';
// import Axis from 'bizcharts/lib/components/Axis';
// import Tooltip from 'bizcharts/lib/components/Tooltip';
// import Geom from 'bizcharts/lib/components/Geom';
// import Legend from 'bizcharts/lib/components/Legend';


import Debounce from 'lodash-decorators/debounce';
import Bind from 'lodash-decorators/bind';
// import DataSet from "@antv/data-set";
import autoHeight from '../autoHeight';
import styles from '../index.less';

track(false);

@autoHeight()
class Bar extends Component {
  state = {
    autoHideXLabels: false,
  };

  componentDidMount() {
    window.addEventListener('resize', this.resize);
  }

  componentWillUnmount() {
    window.removeEventListener('resize', this.resize);
  }

  @Bind()
  @Debounce(400)
  resize() {
    if (!this.node) {
      return;
    }
    const canvasWidth = this.node.parentNode.clientWidth;
    const { data = [], autoLabel = true } = this.props;
    if (!autoLabel) {
      return;
    }
    const minWidth = data.length * 30;


    const { autoHideXLabels } = this.state;

    if (canvasWidth <= minWidth) {
      if (!autoHideXLabels) {
        this.setState({
          autoHideXLabels: true,
        });
      }
    } else if (autoHideXLabels) {
      this.setState({
        autoHideXLabels: false,
      });
    }
  }

  handleRoot = (n) => {
    this.root = n;
  };

  handleRef = (n) => {
    this.node = n;
  };

  render() {
    const {
      height,
      title,
      type="intervalStack",
      forceFit = true,
      data,
      color = 'rgba(24, 144, 255, 0.85)',
      padding,
      loading,
    } = this.props;

    const { autoHideXLabels } = this.state;

    const scale = {
      x: {
        type: 'cat',
      },
      y: {
        min: 0,
      },
    };

    const tooltip = [
      'x*y',
      (x, y) => ({
        name: x,
        value: y,
      }),
    ];

    return (
      <div className={styles.chart} style={{ height}} ref={this.handleRoot}>
        <div ref={this.handleRef}>
          {title && <h4 style={{ marginBottom: 20 }}>{title}</h4>}
          <Chart
            scale={scale}
            height={title ? height - 41 : height}
            // width={330}
            forceFit={true}
            data={data}
            padding={padding || 'auto'}
          >
            <Legend />
            <Axis name="x" />
            <Axis name="y" />
            <Tooltip />
            <Geom
              type={type}
              position="x*y"
              color={"name"}
              style={{
                stroke: "#fff",
                lineWidth: 1
              }}
              adjust={[
                {
                  type: "dodge",
                  marginRatio: 1 / 32
                }
              ]}
            />
          </Chart>
        </div>
      </div>
    );
  }
}

export default Bar;
